package cn.lg.soar.common.util.jackson.deserializer;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;
import java.text.DecimalFormat;
import java.text.ParseException;

/**
 * LocalTime 反序列化
 * @author luguoxiang
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
public class DecimalDeserializer extends JsonDeserializer<Number> {

    private final DecimalFormat format;

    public DecimalDeserializer() {
        this(",###");
    }
    public DecimalDeserializer(String pattern) {
        this(new DecimalFormat(pattern));
    }
    public DecimalDeserializer(DecimalFormat format) {
        this.format = format;
    }

    @Override
    public Number deserialize(JsonParser parser, DeserializationContext context) throws IOException {
        try {
            return format.parse(parser.getText());
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

}
